
/*
struct ListNode {
    int val;
    struct ListNode *next;
    ListNode(int x) :
        val(x), next(NULL) {
    }
};
*/
class Solution {
public:
    ListNode* EntryNodeOfLoop(ListNode* pHead) {
        ListNode* slow = pHead, * fast = pHead;
        bool flag = false;
        while (fast && fast->next)
        {
            fast = fast->next;
            fast = fast->next;
            slow = slow->next;
            if (fast == slow)
            {
                flag = true;
                break;
            }
        }
        if (!flag) return nullptr;
        slow = pHead;
        while (slow != fast)
        {
            slow = slow->next;
            fast = fast->next;
        }

        return slow;
    }
};